﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrogEngine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TestGame
{
	public class TestPlaneModelEntity : ModelGameEntityBase
	{
		public TestPlaneModelEntity(IEngine engine, string assetName)
			: base(engine, assetName)
		{
			this.device = engine.GraphicsDevice;
		}

		GraphicsDevice device;

		public override void Update(GameTime gameTime)
		{
			
		}

		public override void Render(GameTime gameTime)
		{
			device.BlendState = BlendState.Opaque;
			device.DepthStencilState = DepthStencilState.Default;
			device.SamplerStates[0] = SamplerState.LinearWrap;

			ICamera camera = cameraService.GetActiveCamera();

			Matrix[] transforms = new Matrix[Model.Bones.Count];
			Model.CopyAbsoluteBoneTransformsTo(transforms);

			foreach (ModelMesh mesh in Model.Meshes)
			{
				foreach (BasicEffect eff in mesh.Effects)
				{
					//eff.Texture = checker;
					eff.EnableDefaultLighting();
					eff.World = transforms[mesh.ParentBone.Index] * World;

					// Use the matrices provided by the chase camera
					eff.View = camera.View;
					eff.Projection = camera.Projection;
				}

				device.SamplerStates[0] = SamplerState.LinearWrap;
				mesh.Draw();
			}
		}

		public override IEnumerable<string> AssetNames()
		{
			return base.AssetNames();
			//return new List<string> { "Checker" }.Union(base.AssetNames());
		}

		private Texture2D checker;

		public override void LoadContent()
		{
			//this.checker = ContentManager.Load<Texture2D>("Checker");
			Model = ContentManager.Load<Model>(this.modelAsset);
		}
	}
}
